package rpc

import (
	"context"

	"bilinadmin/internal/object"

	"google.golang.org/grpc"

	pb "bilinadmin/api/pb/server"
	"bilinadmin/internal/service"
)

type brandHandler struct {
	*RpcBase
	pb.UnimplementedBilinAdminServer
	name string
}

// NewBrandRpcHandler 初始化 brand rpc handler
func NewBrandRpcHandler() *brandHandler {
	return &brandHandler{
		RpcBase:                       base,
		UnimplementedBilinAdminServer: pb.UnimplementedBilinAdminServer{},
		name:                          "rpc_brand_store_handler",
	}
}

// Register 注册服务 handler
func (b *brandHandler) Register(server *grpc.Server) {
	pb.RegisterBilinAdminServer(server, b)
}

// SayHello hello world test
func (b *brandHandler) SayHello(ctx context.Context, req *pb.HelloRequest) (*pb.HelloResponse, error) {
	if req == nil {
		return nil, nil
	}
	resp, errL := service.GetBrandService().SayHello(ctx, req.Name, req.Id)
	if errL != nil {
		return nil, errL
	}
	return &pb.HelloResponse{
		Message: resp,
	}, nil
}

// BrandCreate 品牌创建
func (b *brandHandler) BrandCreate(ctx context.Context, req *pb.BrandCreateRequest) (*pb.BrandResponse, error) {
	if req == nil {
		return nil, nil
	}
	if req.GetOrigin() == "" {
		req.Origin = "bg"
	}
	acc, errL := service.GetService().VerifyAccountToken(req.AccountToken)
	if errL != nil {
		return nil, errL
	}
	ret, errL := service.GetBrandService().CreateBrand(ctx, req, acc.GID, acc.UserName, acc.Mobile, acc.Email)
	if errL != nil {
		return nil, errL
	}
	return ret, nil
}

// BrandsGet 获取品牌列表
func (b *brandHandler) BrandsGet(ctx context.Context, req *pb.BrandsGetRequest) (*pb.BrandsGetResponse, error) {
	if req == nil {
		return nil, nil
	}
	acc, errL := service.GetService().VerifyAccountToken(req.AccountToken)
	if errL != nil {
		return nil, errL
	}
	return service.GetBrandService().GetBrandsOfAccount(ctx, acc.GID)
}

// BrandGetById 根据id获取品牌信息
func (b *brandHandler) BrandGetById(ctx context.Context, req *pb.BrandGetByIdReq) (*pb.BrandInfoReply, error) {
	if req == nil {
		return nil, nil
	}
	return service.GetBrandService().GetBrandInfoById(ctx, req.BrandId)
}

// StoreGetById 根据id获取门店信息
func (b *brandHandler) StoreGetById(ctx context.Context, req *pb.StoreGetByIdReq) (*pb.StoreInfoReply, error) {
	if req == nil {
		return nil, nil
	}
	return service.GetBrandService().GetStoreInfoById(ctx, req.BrandId, req.StoreId)
}

// StoresGetByBrand 根据品牌信息获取门店列表
func (b *brandHandler) StoresGetByBrand(ctx context.Context, req *pb.StoresGetByBrandReq) (*pb.StoresInfoReply, error) {
	if req == nil {
		return nil, nil
	}
	pagination := object.NewPagination(req.PageNo, req.PageSize)
	return service.GetBrandService().GetStoreListByBrand(ctx, req.BrandId, pagination)
}

// OpenBrandMall 开通商城
func (b *brandHandler) OpenBrandMall(ctx context.Context, req *pb.OpenMallReq) (*pb.OpenMallReply, error) {
	if req == nil {
		return nil, nil
	}
	errL := service.GetBrandService().OpenMall(ctx, req.BrandId, req.EmpId, req)
	if errL != nil {
		if errL.SendMail() {
			return nil, errL
		}
		return &pb.OpenMallReply{Message: errL.Msg()}, nil
	}
	return &pb.OpenMallReply{Message: "success"}, nil
}

// StoreNamesGetByIds 根据ids获取门店名列表
func (b *brandHandler) StoreNamesGetByIds(ctx context.Context, req *pb.StoreNamesGetByIdsReq) (*pb.StoreNamesReply, error) {
	if req == nil {
		return nil, nil
	}
	return service.GetBrandService().GetStoreNamesByIds(ctx, req.StoreIds)
}

// GetStoresByGeo 根据地图坐标获取门店列表
func (b *brandHandler) GetStoresByGeo(ctx context.Context, req *pb.GetStoresByGeoReq) (*pb.GetStoresByGeoReply, error) {
	if req == nil {
		return nil, nil
	}
	return service.GetBrandService().GetStoresByLocation(ctx, req.BrandId, req.Latitude, req.Longitude)
}

// BrandNamesGetByIds 根据id获取品牌信息
func (b *brandHandler) BrandNamesGetByIds(ctx context.Context, req *pb.BrandsGetByIdsReq) (*pb.BrandNamesReply, error) {
	if req == nil {
		return nil, nil
	}
	return service.GetBrandService().GetBrandNamesByIds(ctx, req.BrandIds)
}
